/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { describe, expect, test } from '@jest/globals';
import { outputFileSync } from 'fs-extra';
import * as path from 'path';

import { ApiType } from '../../../src/project/data/hap-extra-info';
import { initByStageEmptyApp, initExecuteCommand, runHvigor, runShouldFail } from '../common/common';
import { ModuleBuildProfileModifier } from '../common/modifier/module-build-profile-modifier';
import { ProjectBuildProfileModifier } from '../common/modifier/project-build-profile-modifier';

/**
 * 校验相关的测试用例，用于测试对配置文件的校验是否正常
 */
describe('Unsorted Validate Test', () => {
  test.concurrent('Target and product name validate', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const moduleBuildProfileModifier = new ModuleBuildProfileModifier(dir, 'entry');
        const projectBuildProfileModifier = new ProjectBuildProfileModifier(dir);

        // target name should not contain space
        moduleBuildProfileModifier.updateTarget({
          name: 'default test'
        }).outputSync();
        const commands = initExecuteCommand({
          mode: 'module',
          tasks: 'Prebuild'
        });
        runShouldFail(dir, commands, 'must match pattern "^[0-9a-zA-Z_-]+$"');

        projectBuildProfileModifier.updateProduct({
          name: 'default test'
        }).outputSync();
        runShouldFail(dir, commands, 'must match pattern "^[\\\\da-zA-Z_-]+$"');
        projectBuildProfileModifier.updateModuleTargetByName({
          name: 'default test',
          applyToProducts: [
            'default'
          ]
        }, 'entry').outputSync();
        runShouldFail(dir, commands, 'must match pattern "^[\\\\da-zA-Z_-]+$"');
      }
    });
  });

  test.concurrent('Test no target to execute in certain product', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const projectBuildProfileModifier = new ProjectBuildProfileModifier(dir);
        projectBuildProfileModifier.updateProduct({
          name: 'product2'
        }).outputSync();

        const commands = initExecuteCommand({
          mode: 'module',
          product: 'product2',
          tasks: 'assembleHap'
        });
        try {
          runHvigor(dir, commands, { stderr: 'pipe' });
        } catch (e: any) {
          expect(e.stderr).toContain('Current product is \'product2\'.No executable target in entry.');
        }
      }
    });
  });

  test.concurrent('hvigorfile.js, apiType and module type consistency', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const moduleBuildProfileModifier = new ModuleBuildProfileModifier(dir, 'entry');
        const hvigorFile = path.resolve(dir, 'entry', 'hvigorfile.js');

        // 修改错误的hvigorfile task
        outputFileSync(hvigorFile, 'module.exports = require("@ohos/hvigor-ohos-plugin").legacyHapTasks');

        const commands = initExecuteCommand({
          mode: 'module',
          tasks: 'Prebuild'
        });
        runShouldFail(dir, commands,
          'Mismatch with apiType.');

        // 修改错误的apiType
        outputFileSync(hvigorFile, 'module.exports = require("@ohos/hvigor-ohos-plugin").hapTasks');
        moduleBuildProfileModifier.setApiType(ApiType.FA)
          .setCompileMode(undefined)
          .outputSync();
        runShouldFail(dir, commands,
          'Verify the settings in the hvigorfile or set apiType in the build-profile.json5 ' +
          'file to \'stageMode\'.');

        outputFileSync(hvigorFile, 'module.exports = require("@ohos/hvigor-ohos-plugin").harTasks');
        moduleBuildProfileModifier.setApiType(ApiType.STAGE).outputSync();
        runShouldFail(dir, commands, 'Make sure the module type set in build-profile.json5 ' +
          'is consistent with the settings of hvigorfile.');
      }
    });
  });
});
